set:- use_module('types/syntax').

:- consult([
      'Ricerca/astar',
      agenti
   ]).

:- maplist(writeln,[
'\n-------------------------------------------------------\n',
'set_map(<nomefile>)    (carica la mappa dal livello)',
'find_path(<posizione iniziale>, <attrezzatura>, -Sol)

       il file livello  contiene l''unico livello attualmente creato
       caricato il livello con set_map(livello), provare con, ad esempio:
       find_path(p2(1,1),scarpetta,Sol).
       find_path(p2(1,3),scarpetta,Sol).
       e con scarpone, ecc.',

'\n-------------------------------------------------------'
		   ]).
		   

% ------------------------------------------ FUNZIONALITA' interfaccia

set_map(Map) :- 
	working_directory(_,'//home//alex//workspace3.8//Wumpus'),
	%working_directory(_,'Z:\\Documents\\Workspace\\Wumpus'),
	%working_directory(_,'Z:\\Documents\\Workspace\\Wumpus_SVN'),
	load_map(Map).

find_path(Sol) :-
	start(p(1,1),ST),
	writeln(ST),
	nl,
	time(solve(ST,Sol)),
%	solve(ST,Sol),
	nl,
	writeln('Sol':Sol),
	show_sol(lista,Sol).

%-------------------------------------    MOSTRA il path

show_sol(lista,nc(SA,Path,Cost)) :-
	writeln(costo:Cost),
	nl,
	writeln('----------------------------------'),
	reverse([SA|Path], RPath),
	maplist(writeln,RPath),
	writeln('-----------------------------------').

% --------------------------------------  Mostra il percorso come mappa
%
show_sol(mappa,nc(SA, Path, Cost)) :-
	writeln(costo:Cost),
	writeln('-----------mappa------------'),
	dim(DX,DY),
	forall(between(1,DY,Y),
	      (	forall(between(1,DX,X), write_agent(X,Y,[SA|Path])), nl )),
	writeln('----------------------------'),
	nl,
	reverse([SA|Path],RPath),
	maplist(write_pos, RPath),
	nl,
	nl.

write_pos(stato(_,p2(X,Y),_,T)) :-
	length(T,N),
	write('-->'),
	write((X,Y):N).

write_agent(X,Y,L) :-
	member(stato(_, p2(X,Y), _, T),L) ->
	       length(T,N),
	       write(N)
	       ;
	       write(' ').

% -------- sovrascrive taglia_cicli

/*taglia_cicli(stato(_,P,S,T),_) :-
           visited(st(P,S,T)),!
	   ;
	   assert(visited(st(P,S,T))),
	   fail.*/